#if (FEATURE_MODE == 0)
/**
 @file ctc_at_trill.c

 @date 2015-10-26

 @version v3.0

*/
/****************************************************************************
 *
* Header Files
*
****************************************************************************/
#include "ctc_const.h"
#include "ctc_error.h"

#include "ctc_trill.h"
#include "sys_usw_trill.h"
#include "sys_usw_common.h"

/****************************************************************************
 *
* Defines and Macros
*
*****************************************************************************/

/****************************************************************************
 *
* Global and Declaration
*
*****************************************************************************/
/****************************************************************************
 *
* Function
*
*****************************************************************************/


int32
ctc_at_trill_init(uint8 lchip, void* trill_global_cfg)
{
    uint8 lchip_start = 0;
    uint8 lchip_end = 0;

    LCHIP_CHECK(lchip);
    CTC_AT_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_trill_init(lchip, trill_global_cfg));
    }

    return CTC_E_NONE;
}

int32
ctc_at_trill_deinit(uint8 lchip)
{
    uint8 lchip_start = 0;
    uint8 lchip_end = 0;
    
    LCHIP_CHECK(lchip);
    CTC_AT_FOREACH_LCHIP(lchip_start, lchip_end, 1)
    {
        CTC_ERROR_RETURN(sys_usw_trill_deinit(lchip));
    }

    return CTC_E_NONE;
}

int32
ctc_at_trill_add_route(uint8 lchip, ctc_trill_route_t* p_trill_route)
{
    uint8 lchip_start = SYS_PP_BASE(lchip);
    uint8 lchip_end = lchip_start + 1;
    uint16 pp_bmp = 0;
    int32 ret = CTC_E_NONE;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_TRILL);
    LCHIP_CHECK(lchip);
    CTC_PTR_VALID_CHECK(p_trill_route);

    pp_bmp = SYS_PP_BMP_MAPPING(p_trill_route->pp_bmp);
    if (CTC_FLAG_ISSET(p_trill_route->flag, CTC_TRILL_ROUTE_FLAG_SRC_PORT_CHECK)
        || (CTC_FLAG_ISSET(p_trill_route->flag, CTC_TRILL_ROUTE_FLAG_MCAST)
            && CTC_FLAG_ISSET(p_trill_route->flag, CTC_TRILL_ROUTE_FLAG_MACSA_CHECK)))
    {
        if (MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_SCL))
        {
            lchip_end = lchip_start + SYS_PP_NUM(lchip);
        }
        
    }
    else if (MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_IPUC))
    {
        lchip_end = lchip_start + SYS_PP_NUM(lchip);
    }
    
    for(lchip = lchip_start; lchip < lchip_end; lchip++)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        CTC_AT_FOREACH_ERROR_RETURN(ret, sys_usw_trill_add_route(lchip, p_trill_route));
    }

    /*rollback if error exist*/
    CTC_AT_FOREACH_ROLLBACK(lchip_start,lchip_end)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        sys_usw_trill_remove_route(lchip, p_trill_route);
    }

    return ret;
}

int32
ctc_at_trill_remove_route(uint8 lchip, ctc_trill_route_t* p_trill_route)
{
    uint8 lchip_start = SYS_PP_BASE(lchip);
    uint8 lchip_end = lchip_start + 1;
    uint16 pp_bmp = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_TRILL);
    LCHIP_CHECK(lchip);
    CTC_PTR_VALID_CHECK(p_trill_route);

    pp_bmp = SYS_PP_BMP_MAPPING(p_trill_route->pp_bmp);
    if (CTC_FLAG_ISSET(p_trill_route->flag, CTC_TRILL_ROUTE_FLAG_SRC_PORT_CHECK)
        || CTC_FLAG_ISSET(p_trill_route->flag, CTC_TRILL_ROUTE_FLAG_MACSA_CHECK))
    {
        if (MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_SCL))
        {
            lchip_end = lchip_start + SYS_PP_NUM(lchip);
        }
    }
    else if (MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_IPUC))
    {
        lchip_end = lchip_start + SYS_PP_NUM(lchip);
    }
        
    for(lchip = lchip_start; lchip < lchip_end; lchip++)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        CTC_ERROR_RETURN(sys_usw_trill_remove_route(lchip, p_trill_route));
    }
    return CTC_E_NONE;
}

int32
ctc_at_trill_add_tunnel(uint8 lchip, ctc_trill_tunnel_t* p_trill_tunnel)
{
    uint8 lchip_start = 0;
    uint8 lchip_end = 0;
    uint8 all_lchip = 1;
    uint16 pp_bmp = 0;
    int32 ret = CTC_E_NONE;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_TRILL);
    LCHIP_CHECK(lchip);
    CTC_PTR_VALID_CHECK(p_trill_tunnel);

    pp_bmp = SYS_PP_BMP_MAPPING(p_trill_tunnel->pp_bmp);
    if(!MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_SCL))
    {
        return sys_usw_trill_add_tunnel(SYS_PP_BASE(lchip), p_trill_tunnel);
    }

    CTC_AT_FOREACH_LCHIP(lchip_start, lchip_end, all_lchip)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        CTC_AT_FOREACH_ERROR_RETURN(ret, sys_usw_trill_add_tunnel(lchip, p_trill_tunnel));
    }

    CTC_AT_FOREACH_ROLLBACK(lchip_start, lchip_end)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        sys_usw_trill_remove_tunnel(lchip, p_trill_tunnel);
    }

    return ret;
}

int32
ctc_at_trill_remove_tunnel(uint8 lchip, ctc_trill_tunnel_t* p_trill_tunnel)
{
    uint8 lchip_start = 0;
    uint8 lchip_end = 0;
    uint8 all_lchip = 1;
    uint16 pp_bmp = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_TRILL);
    LCHIP_CHECK(lchip);
    CTC_PTR_VALID_CHECK(p_trill_tunnel);

    pp_bmp = SYS_PP_BMP_MAPPING(p_trill_tunnel->pp_bmp);
    if(!MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_SCL))
    {
        return sys_usw_trill_remove_tunnel(SYS_PP_BASE(lchip), p_trill_tunnel);
    }

    CTC_AT_FOREACH_LCHIP(lchip_start, lchip_end, all_lchip)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))    
        {
            continue;
        }
        CTC_ERROR_RETURN(sys_usw_trill_remove_tunnel(lchip, p_trill_tunnel));
    }
    return CTC_E_NONE;
}


int32
ctc_at_trill_update_tunnel(uint8 lchip, ctc_trill_tunnel_t* p_trill_tunnel)
{
    uint8 lchip_start = 0;
    uint8 lchip_end = 0;
    uint8 all_lchip = 1;
    uint16 pp_bmp = 0;

    FEATURE_SUPPORT_CHECK(CTC_FEATURE_TRILL);
    LCHIP_CHECK(lchip);
    CTC_PTR_VALID_CHECK(p_trill_tunnel);
    pp_bmp  = SYS_PP_BMP_MAPPING(p_trill_tunnel->pp_bmp);
    if(!MCHIP_FEATURE_PP_EN(lchip, CTC_FEATURE_SCL))
    {
        return sys_usw_trill_update_tunnel(SYS_PP_BASE(lchip), p_trill_tunnel);
    }

    CTC_AT_FOREACH_LCHIP(lchip_start, lchip_end, all_lchip)
    {
        if (!CTC_IS_BIT_SET(pp_bmp, lchip-lchip_start))
        {
            continue;
        }
        CTC_ERROR_RETURN(sys_usw_trill_update_tunnel(lchip, p_trill_tunnel));
    }
    return CTC_E_NONE;
}

#endif

